﻿using B.S.Sterilizationcenter.Domain.zmc;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Read.API.Appliaction.Commends.zmc.Warehou;
using B.S.Sterilizationcenter.Read.API.Dto;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Read.API.Appliaction.CommandHandlers.zmc.Warehou
{

    
    public class QueryStuffstuffbarcodeCommendHandlers : IRequestHandler<QueryStuffstuffbarcodeCommends, ApiResult<List<StuffCleaningStuffRelationDto>>>
    {

        private readonly ISqlSugarClient db;

        public QueryStuffstuffbarcodeCommendHandlers(ISqlSugarClient db)
        {
            this.db = db;
        }

        /// <summary>
        /// 根据条码查询物品
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<List<StuffCleaningStuffRelationDto>>> Handle(QueryStuffstuffbarcodeCommends request, CancellationToken cancellationToken)
        {
            ApiResult<List<StuffCleaningStuffRelationDto>> result = new ApiResult<List<StuffCleaningStuffRelationDto>>();
            result.Code = ApiEnums.查询成功;
            result.Msg = "查询成功";
         var list = db.Queryable<Stuff>().LeftJoin<CleaningStuffRelation>((a, b) => a.StuffId == b.StuffId).Select((a, b) =>new StuffCleaningStuffRelationDto
         {
             StuffName = a.StuffName,
             StuffId = a.StuffId,
             Stuffbarcode = a.Stuffbarcode,
             Quantity = b.Quantity
         }).ToList();
            if (!string.IsNullOrEmpty(request.Stuffbarcode)) {
                list = list.Where(a => a.Stuffbarcode.Contains(request.Stuffbarcode)).ToList();
            }
         result.Data = list;
            return result;
        }
    }
}
